<!DOCTYPE html>
<html class="x-admin-sm">
    
    <head>
        <meta charset="UTF-8">
        <title>欢迎页面-X-admin2.2</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <link rel="stylesheet" href="../../../static/css/font.css">
        <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
        <link rel="stylesheet" href="../../../static/css/xadmin.css">
        <script type="text/javascript" src="../../../static/lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="../../../static/js/xadmin.js"></script>
        <script src="../../../static/js/axios.min.js" charset="utf-8"></script>
        <script src="../../../js/api/agency.js" charset="utf-8"></script>
        <script src="../../../js/api/group.js" charset="utf-8"></script>
        <script src="../../../js/api/role.js" charset="utf-8"></script>
        <script src="../../../js/api/user.js" charset="utf-8"></script>
        <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
        <!--[if lt IE 9]>
            <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
            <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    <body>
        <div class="layui-fluid">
            <div class="layui-row">
                <form class="layui-form">
                  <div class="layui-form-item">
                      <label for="userName" class="layui-form-label">
                          用户名称
                      </label>
                      <div class="layui-input-inline">
                          <input type="text" id="userName" name="userName"   disabled
                          autocomplete="off" class="layui-input">
                      </div>
                      <label for="grouplist" class="layui-form-label">
                          所属部门</label>
                      <div class="layui-input-inline">
                          <select id="grouplist" lay-filter="groupselect">
                              <option value="">请选择部门</option>
                          </select>
                      </div>
                  </div>
                    <div class="layui-form-item">
                        <div class="layui-card">
                            <div class="layui-card-header">职务列表</div>
                            <div class="layui-card-body">
                                <div id="role" class="demo-transfer"></div>
                            </div>
                        </div>
                    </div>


                  <div class="layui-form-item">
                      <label for="userName" class="layui-form-label">
                      </label>
                      <button  class="layui-btn" lay-filter="add" lay-submit="">
                          保存
                      </button>
                      <button  class="layui-btn" id="close">
                          关闭
                      </button>
                  </div>
              </form>
            </div>
        </div>
        <script>
            layui.config({
                base: './../../../static/lib/dist/'
            })

            layui.use(['form', 'layer', 'selectPlus', 'notice', 'transfer', 'util'],
            function() {
                $ = layui.jquery;
                var form = layui.form,
                transfer = layui.transfer,
                    util = layui.util,
                selectPlus = layui.selectPlus,
                notice = layui.notice,
                layer = layui.layer;


                new Promise((resolve, reject) => {
                    let ugroup = layui.data('userinfo').assign_role.groupInfos
                    $('#userName').val(layui.data('userinfo').assign_role.name)
                    if(ugroup!=null && ugroup.length>0){
                        let group = $('#grouplist')
                        ugroup.forEach(ginfo=>{
                            group.append('<option value="'+ginfo.groupId+'">'+ginfo.groupName+'</option>')
                        })
                    }
                    form.render();
                    resolve('')
                }).then(vl => {
                    //初始右侧数据
                    transfer.render({
                        elem: '#role',
                        title: ['可选职务', '已分配职务']
                        ,id: 'roletran' //定义唯一索引
                        ,data: []
                        ,value: []
                    })
                }).catch(reason => {
                        notice.error('部门信息获取失败')
                })

                //监听部门选择
                form.on('select(groupselect)', function(data){
                    if(data.value == ''){
                        transfer.reload('roletran', {
                            value: [],
                            data: []
                        })
                        return false
                    }
                    //获取部门下的职务 以及 用户在该部门下的职务
                    searchRolesOfGroup(data.value).then(value => {
                        if(value.code == 200){
                            new Promise((resolve, reject) => {
                                let aroles = []
                                if(value.data != null){
                                    value.data.forEach(ar=>{
                                        aroles.push({
                                            title: ar.roleName,
                                            value: ar.id
                                        })
                                    })
                                }
                                resolve(aroles)
                            }).then(value1 => {
                                searchRolesOfUser(data.value, layui.data('userinfo').assign_role.id).then(value2 => {
                                    if(value2.code == 200){
                                        let yroles = []
                                        if(value2.data != null){
                                            value2.data.forEach(v2=>{
                                                yroles.push(v2.id)
                                            })
                                        }
                                        transfer.reload('roletran', {
                                            value: yroles,
                                            data: value1
                                        })
                                    }else{
                                        notice.error('获取用户职务信息失败，'+value.msg)
                                    }
                                }).catch(reason => {
                                    notice.error('获取用户职务信息失败,服务器未连接')
                                })
                            })

                        }else{
                            notice.error('获取部门下职务信息失败，'+value.msg)
                        }
                    }).catch(reason => {
                        notice.error('获取部门下职务信息失败,服务器未连接')
                    })
                })



                //监听提交
                form.on('submit(add)',
                function(data) {
                    let roles = []
                    var getData = transfer.getData('roletran'); //获取右侧数据
                    getData.forEach(gd =>{
                        roles.push({
                            id: gd.value
                        })
                    })
                    assignedRole(layui.data('userinfo').assign_role.id, $('#grouplist').val(), roles)
                        .then(value => {
                        if(value.code == 200){
                            layer.msg("保存成功", {
                                icon: 6
                            })
                        }else{
                            notice.error('保存信息失败，'+value.msg)
                        }
                    }).catch(reason => {
                        notice.error('服务器未连接')
                    })

                    return false;
                });

                $('#close').click(function () {
                    //关闭当前frame
                    xadmin.close();
                    // 可以对父窗口进行刷新
                    xadmin.father_reload();
                })

            });</script>
        <script>var _hmt = _hmt || []; (function() {
                var hm = document.createElement("script");
                hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
                var s = document.getElementsByTagName("script")[0];
                s.parentNode.insertBefore(hm, s);
            })();</script>
    </body>

</html>
